<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('recharges', function (Blueprint $table) {
            // 主键 - 雪花ID
            $table->unsignedBigInteger('id')->primary()->comment('充值ID(雪花ID)');
            
            // 关联字段
            $table->unsignedBigInteger('tenant_id')->comment('租户ID');
            
            // 充值信息
            $table->string('recharge_number', 50)->comment('充值单号');
            $table->string('landing_company', 200)->comment('落地公司');
            $table->decimal('amount', 15, 2)->comment('充值金额');
            $table->string('bank_name', 100)->comment('充值银行名称');
            $table->string('bank_account', 50)->comment('充值银行卡号');
            
            // 状态管理（直接使用中文枚举）
            $table->enum('status', [
                '待处理',
                '处理中',
                '已匹配',
                '已确认',
                '失败',
                '已取消',
                '充值中',
                '充值失败',
                '已到账',
                '已结清',
                '部分退账',
                '已退账',
            ])->default('待处理')->comment('状态');

            $table->string('status_description', 500)->nullable()->comment('状态说明');
            $table->string('matched_detail_id', 255)->nullable()->comment('匹配的银行明细ID');
            $table->string('matched_bank', 50)->nullable()->comment('匹配的银行代码');
            $table->decimal('match_confidence', 3, 2)->default(0.00)->comment('匹配置信度 0.00-1.00');
            $table->unsignedBigInteger('admin_user_id')->nullable()->comment('处理管理员ID');

            // 备注信息
            $table->text('remarks')->nullable()->comment('备注');

            // 时间戳字段
            $table->timestamp('created_at')->nullable()->comment('创建时间');
            $table->timestamp('updated_at')->nullable()->comment('更新时间');
            $table->timestamp('processed_at')->nullable()->comment('开始处理时间');
            $table->timestamp('matched_at')->nullable()->comment('匹配时间');
            $table->timestamp('arrived_at')->nullable()->comment('到账时间');
            $table->timestamp('settled_at')->nullable()->comment('结清时间');
            $table->timestamp('refunded_at')->nullable()->comment('退账时间');
            $table->timestamp('confirmed_at')->nullable()->comment('确认时间');

            // 操作人员
            $table->unsignedBigInteger('created_by')->nullable()->comment('创建人ID');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('更新人ID');

            // 外键约束
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            $table->foreign('admin_user_id')->references('id')->on('admin_users')->onDelete('set null');

            // 索引
            $table->index(['tenant_id', 'status'], 'idx_tenant_status');
            $table->index(['tenant_id', 'landing_company'], 'idx_tenant_landing_company');
            $table->index(['tenant_id', 'created_at'], 'idx_tenant_created_at');
            $table->index(['tenant_id', 'recharge_number'], 'idx_tenant_recharge_number');
            $table->index(['amount', 'bank_name'], 'idx_amount_bank_new');
            $table->index(['status', 'created_at'], 'idx_status_created');
            $table->index(['matched_bank', 'matched_detail_id'], 'idx_matched_detail');
            $table->index(['confirmed_at'], 'idx_confirmed_at');

            // 租户级唯一约束
            $table->unique(['tenant_id', 'recharge_number'], 'uk_tenant_recharge_number');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('recharges');
    }
};
